package netty.server;

import java.net.InetSocketAddress;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * 1.设置端口值（抛出一个 NumberFormatException 如果该端口参数的格式不正确） 
 * 2.呼叫服务器的 start() 方法 
 * 3.创建EventLoopGroup 
 * 4.创建 ServerBootstrap 
 * 5.指定使用 NIO 的传输 Channel 
 * 6.设置 socket地址使用所选的端口 
 * 7.添加 EchoServerHandler 到 Channel 的 ChannelPipeline 
 * 8.绑定的服务器;sync等待服务器关闭 
 * 9.关闭 channel 和 块，直到它被关闭 
 * 10.关机的 EventLoopGroup，释放所有资源。
 */
public class EchoServer {

	private final int port;

	public EchoServer(int port) {
		this.port = port;
	}

	public static void main(String[] args) throws Exception {
//		if (args.length != 1) {
//			System.err.println("Usage: " + EchoServer.class.getSimpleName() + " <port>");
//			return;
//		}
		int port = 8080;//Integer.parseInt(args[0]); // 1
		new EchoServer(port).start(); // 2
	}

	public void start() throws Exception {
		EventLoopGroup group = new NioEventLoopGroup(); // 3
		try {
			ServerBootstrap b = new ServerBootstrap();
			b.group(group) // 4
					.channel(NioServerSocketChannel.class) // 5
					.localAddress(new InetSocketAddress(port)) // 6
					.childHandler(new ChannelInitializer<SocketChannel>() { // 7
						@Override
						public void initChannel(SocketChannel ch) throws Exception {
							ch.pipeline().addLast(new EchoServerHandler());
						}
					});

			ChannelFuture f = b.bind().sync(); // 8
			System.out.println(EchoServer.class.getName() + " started and listen on " + f.channel().localAddress());
			f.channel().closeFuture().sync(); // 9
		} finally {
			group.shutdownGracefully().sync(); // 10
		}
	}

}